const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level:"",
    brandid:"",
    list:[],
    isHaveNewItem:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      level:app.globalData.level,
      brandid:app.globalData.brandid,
    })
    this.getList();
  },
  getList(){
    const that = this
    wx.cloud.callFunction({
      name: 'getScanCodeList',
      data: {
        level:app.globalData.myLevel,
        brandid:app.globalData.myBrandid
      }
    }).then(res => {
      console.log(res)
      res.result.forEach((item,index)=>{
        if(!item.isSubmit){
          this.setData({
            isHaveNewItem:true
          })
        }else{
          this.setData({
            isHaveNewItem:false
          })
        }
        item.scanCodeTime=this.setTime(item.scanCodeTime)
      })
      that.setData({
        list:res.result
      })
    }) 
  },
  setTime(time){
    var newDate = new Date(time*1000 ); 
    var y = newDate.getFullYear()
    var m = newDate.getMonth() + 1
    m = m < 10 ? '0' + m : m
    var d = newDate.getDate()
    d = d < 10 ? ('0' + d) : d
    return y+"-"+m+"-"+d
  },
  submit(){
    var idList=[]
    this.data.list.forEach((item,index)=>{
      idList.push(item.device_logicid)
    })
    console.log(idList)
    wx.cloud.callFunction({
      name: 'addSubordinate',
      data: {
        brandid:app.globalData.myBrandid,
        subordinateDeviceId:idList
      }
    }).then(res => {
      wx.cloud.callFunction({
        name: 'submit',
        data: {
          level:app.globalData.myLevel,
          brandid:app.globalData.myBrandid
        }
      }).then(res => {
        if(res.result.stats.updated>0){
          wx.showToast({
            title: '提交成功',
            icon:'success'
          })
          
          this.getList();
        }
      }) 
    }) 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})